//
//  CHDmyCollectionVC.m
//  IOSWeiiPhoneNews
//
//  Created by ibokan on 14-4-24.
//  Copyright (c) 2014年 chen. All rights reserved.
//

#import "CHDmyCollectionVC.h"
#import "CHDDetailsVC.h"

@interface CHDmyCollectionVC ()

@end

@implementation CHDmyCollectionVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title=@"本地收藏";
    self.table=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 568) style:UITableViewStylePlain];
    self.table.delegate=self;
    self.table.dataSource=self;
    [self.view addSubview:self.table];
    NSString *documentPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *fileName=[documentPath stringByAppendingPathComponent:@"News.plist"];
    self.arry=[NSMutableArray arrayWithContentsOfFile:fileName];
    if (self.arry.count<=0) {
        self.arry=[NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"news" ofType:@"plist"]];
    }
    UIBarButtonItem *button=[[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStyleBordered target:self action:@selector(edit)];
    self.navigationItem.rightBarButtonItem=button;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma tableview
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.arry.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *string=@"cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:string];
    }
    cell.textLabel.text=[[self.arry objectAtIndex:indexPath.row]objectAtIndex:0];
    cell.textLabel.font=[UIFont systemFontOfSize:14];
    cell.detailTextLabel.text=[[self.arry objectAtIndex:indexPath.row]objectAtIndex:1];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CHDDetailsVC *detail=[[CHDDetailsVC alloc]init];
    detail.urlStr=[[self.arry objectAtIndex:indexPath.row]objectAtIndex:1];
    detail.authorr=[[self.arry objectAtIndex:indexPath.row]objectAtIndex:2];
    detail.timeStr=[[self.arry objectAtIndex:indexPath.row]objectAtIndex:3];
    [self.navigationController pushViewController:detail animated:YES];
    
}
//编辑模式
-(void)edit
{
    [self.table setEditing:!self.table.editing animated:YES];
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle==UITableViewCellEditingStyleDelete) {
        NSString*documents=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
        NSString*path=[documents stringByAppendingPathComponent:@"NewsConformation.plist"];
        self.arry=[[NSMutableArray alloc]initWithContentsOfFile:path];
        [self.arry removeObjectAtIndex:indexPath.row];
        [self.arry writeToFile:path atomically:YES];
        [self.table deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    
}

@end
